package com.kznnc.data_structure.current;

import com.kznnc.data_structure.entity.User;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;

/**
 * @Description  中断使用
 * @Author houchenkai
 * @Created Date: 2021/7/2 11:01
 * @ClassName
 */
@SuppressWarnings("all")
public class InterruptDemo {


    private static  User user = new User("1",1);

    public static void main(String[] args) {


        Thread main = currentThread();
        InterruptRunner interruptRunner = new InterruptRunner(main);
        new Thread(interruptRunner).start();
        try {
            System.out.println("main线程开始进行Thread.sleep=======");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("main线程中断异常");
        }


        try {
            main.join(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("main.join中断异常");
        }
         // main.interrupt();

        while (true){

        }
      // System.out.println(main.isInterrupted());
       // System.out.println(main.interrupted());
//
//        System.out.println(main.interrupted());
//        System.out.println(main.isInterrupted());
    }


       static class InterruptRunner implements Runnable{

        private Thread main;

           public InterruptRunner(Thread main) {
               this.main = main;
           }

           @Override
           public void run() {
               try {
                   Thread.sleep(500);
               } catch (InterruptedException e) {
                   System.out.println("InterruptRunner----------");
                   e.printStackTrace();
               }
               System.out.println("子线程开始进行中断标志----------");
               long  future = System.currentTimeMillis() +3000l;
               while (System.currentTimeMillis() <future ){
                   main.interrupt();
                  // System.out.println("===main线程当前中断状态:"+main.isInterrupted());
               }
           }
       };








}
